package com.example.commonlib.commonutil;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import java.util.ArrayList;
import java.util.List;

public class ApkManageUtil {
    /**
     * apk管理工具
     *
     * @author Frank
     * @version 1.0 Create by 2015.10.8
     */

    /**
     * 是否存在某个应用
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        List<String> pName = new ArrayList<>();
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }

    /**
     * 是否存在某个意图
     *
     * @param context
     * @param action
     * @return
     */
    public static boolean isIntentExisting(Context context, String action) {
        final PackageManager packageManager = context.getPackageManager();
        final Intent intent = new Intent(action);
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);


        return resolveInfo != null && resolveInfo.size() > 0;
    }

    /**
     * 用来判断服务是否运行.
     *
     * @param context
     * @param className 判断的服务名字
     * @return true 在运行 false 不在运行
     */
    public static boolean isServiceRunning(Context context, String className) {
        boolean isRunning = false;
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(200);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            if (serviceList.get(i).service.getClassName().equals(className)) {
                isRunning = true;
                break;
            }
        }
        return isRunning;
    }

    /**
     * 判断app是否运行.
     *
     * @param context
     * @param pkgName 程序包名
     * @return true 在运行 false 不在运行
     */
    public static boolean isAppRunning(Context context, String pkgName) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> list = am.getRunningTasks(100);
        boolean isAppRunning = false;
        for (RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(pkgName)
                    || info.baseActivity.getPackageName().equals(pkgName)) {
                isAppRunning = true;
                break;
            }
        }
        return isAppRunning;
    }

//    /**
//     * 返回当前程序版本名
//     */
//    public static String getAppVersionName(Context context) {
//        try {
//            // ---get the package info---
//            PackageManager pm = context.getPackageManager();
//            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
//            return pi.versionName;
//        } catch (Exception e) {
//        }
//        return "";
//    }
//
//    /**
//     * 返回当前版本号
//     * @param context
//     * @return
//     */
//    public static int getAppVersionCode(Context context) {
//        PackageManager pm = context.getPackageManager();
//        try {
//            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
//            return info.versionCode;
//        } catch (PackageManager.NameNotFoundException e) {
//        }
//        return -1;
//    }

}
